﻿using Client.Constant;
using Client.Enum;
using Client.Framework.Event;
using Client.Framework.Message;
using Client.Framework.Socket;
using Client.Message.Request;
using Client.Message.Response;
using Client.Model;
using Client.View;
using Client.View.MenuView;
using MaterialDesignThemes.Wpf;
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Client.ViewModel.MenuViewModel
{
    /// <summary>
    /// Author: Leil
    /// Description: MenuEditViewModel
    /// CreateTime: 2020/5/28 22:53:39
    /// </summary>
    public class MenuEditViewModel : BindableBase
    {

        public MenuEdit Page { get; private set; }

        public MenuEditViewModel(MenuEdit page)
        {
            Page = page;

            SaveCommand = new DelegateCommand(SaveInfo);

            MsgEventSubscribe();
        }

        private void MsgEventSubscribe()
        {
            MessageEventPublisher.SubscribeEvent(MessageId.OPT_RESP, ReceiveOptMsg);
        }


        private MainMenu selectedParentMenu;

        public MainMenu SelectedParentMenu
        {
            get { return selectedParentMenu; }
            set
            {
                selectedParentMenu = value;
                RaisePropertyChanged("SelectedParentMenu");
            }
        }



        /// <summary>
        /// 初始化
        /// </summary>
        public void Init(ViewMenu menu, MenuListViewModel parentDataContext)
        {
            ParentDataContext = parentDataContext;
            Title = menu == null ? "新增菜单" : "编辑菜单";
            if (menu == null)
            {
                ViewMenu = new ViewMenu();
                return;
            }
            ViewMenu = menu;
        }

        /// <summary>
        /// 标题
        /// </summary>
        private string title;
        public string Title
        {
            get { return title; }
            set
            {
                title = value;
                RaisePropertyChanged("Title");
            }
        }


        private List<MainMenu> topMenus;

        public List<MainMenu> TopMenus
        {
            get { return topMenus; }
            set
            {
                topMenus = value;
                RaisePropertyChanged("TopMenus");
            }
        }


        private ViewMenu menu;

        public ViewMenu ViewMenu
        {
            get { return menu; }
            set
            {
                // 给选中的菜单赋值
                SelectedParentMenu = TopMenus.Find(topMenu =>
                {
                    return value.ParentId == topMenu.ID;
                });
                if (SelectedParentMenu == null)
                {
                    SelectedParentMenu = TopMenus[0];
                }
                menu = value;
                RaisePropertyChanged("ViewMenu");
            }
        }



        /// <summary>
        /// 父级页面的DataContext
        /// </summary>
        private MenuListViewModel ParentDataContext { get; set; }

        /// <summary>
        /// 保存角色信息
        /// </summary>
        public DelegateCommand SaveCommand { get; private set; }
        public void SaveInfo()
        {
            this.ViewMenu.ParentId = SelectedParentMenu.ID;
            AddEditMenuReq msg = new AddEditMenuReq(MessageId.ADD_EDIT_MENU)
            {
                Menu = ViewMenu
            };
            ApplicationClient.Instance().Send(msg);
        }

        /// <summary>
        /// 操作回复消息接收
        /// </summary>
        /// <param name="message">回复消息</param>
        private void ReceiveOptMsg(ResponseBaseMessage message)
        {
            if (message is OperationResp msg)
            {
                if (msg.OptType != (int)OptType.ADD_EDIT_MENU)
                {
                    return;
                }
                if (msg.Code == 200)
                {
                    ParentDataContext.GetTopMenus();
                }
                System.Windows.Application.Current.Dispatcher.Invoke(new Action(() =>
                {
                    ConfirmTip tip = new ConfirmTip()
                    {
                        Message = { Text = msg.Content }
                    };
                    DialogHost.Show(tip, "MenuEditPage", CloseTip);
                }));
            }
        }


        public void CloseTip(object sender, DialogClosingEventArgs eventArgs)
        {
            Page.Hide();
            if (ParentDataContext != null)
            {
                ParentDataContext.GetMenus();
            }
        }


    }
}
